package com.shawn.appupdater.updater.ui;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.shawn.appupdater.R;
import com.shawn.appupdater.updater.AppUpdater;
import com.shawn.appupdater.updater.bean.DownloadBean;
import com.shawn.appupdater.updater.net.INetDownloadCallBack;
import com.shawn.appupdater.updater.utils.AppUtils;

import java.io.File;

/**
 * 弹窗提示用户升级
 */
public class UpdaterVersionShowDialog extends DialogFragment {
    private TextView tv_dialog_title, tv_dialog_content;
    private Button btn_dialog_updater;

    private static final String KEY_DOWALOAD = "download_bean";
    private DownloadBean mDownloadBean;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mDownloadBean = (DownloadBean) bundle.getSerializable(KEY_DOWALOAD);
        }
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_updater, container, false);
        initView(view);
        if (mDownloadBean != null) {
            tv_dialog_title.setText(mDownloadBean.getTitle());
            tv_dialog_content.setText(mDownloadBean.getContent());
            btn_dialog_updater.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    // 点击之后关闭按钮点击
                    view.setEnabled(false);
                    final File targetFile = new File(getActivity().getCacheDir(), "target.apk");
                    AppUpdater.getInstance().getiNetManager().download(mDownloadBean.getUrl(), targetFile, new INetDownloadCallBack() {
                        @Override
                        public void success(File apkFile) {
                            view.setEnabled(true);
                            dismiss();
                            // 安装的代码
                            // todo Check md5
                            String fileMD5 = AppUtils.getFileMD5(targetFile);
//                            Log.d("shawn", "fileMD5: " + fileMD5);
                            if (fileMD5 != null && fileMD5.equals(mDownloadBean.getMd5())) {
                                // 这里可以做一个优化，本地如果已经有文件，做一个MD5校验，就不需要再下载了
                                AppUtils.installApk(getActivity(), apkFile);
                            } else {
                                Toast.makeText(getActivity(), "MD5校验失败", Toast.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void progress(int progress) {
                            // 更新界面的代码6
                            btn_dialog_updater.setText(progress + "%");
                        }

                        @Override
                        public void failed(Throwable throwable) {
                            view.setEnabled(true);
                            Toast.makeText(getActivity(), "文件下载失败", Toast.LENGTH_SHORT).show();

                        }
                    });
                }
            });
        }
        return view;
    }

    private void initView(View view) {
        tv_dialog_title = view.findViewById(R.id.tv_dialog_title);
        tv_dialog_content = view.findViewById(R.id.tv_dialog_content);
        btn_dialog_updater = view.findViewById(R.id.btn_dialog_updater);
    }

    public static void show(FragmentActivity fragmentActivity, DownloadBean downloadBean) {
        Bundle mBundle = new Bundle();
        mBundle.putSerializable(KEY_DOWALOAD, downloadBean);
        UpdaterVersionShowDialog updaterVersionShowDialog = new UpdaterVersionShowDialog();
        updaterVersionShowDialog.setArguments(mBundle);
        updaterVersionShowDialog.show(fragmentActivity.getSupportFragmentManager(), "updaterVersionDialog");
    }
}
